import req from '@/utils/request';
export default {
  data() {
    return {
      tableData: [],
      isLoading: false,
      queryOption: null,
    };
  },
  async created() {
    await this.checkOption();
    this.loadByPage();
  },
  methods: {
    async doSubmit(option) {
      const res = await req({
        url: this.queryOption.submit.url,
        method: this.queryOption.submit.method,
        ...option,
      });
      this.loadByPage();
    },
    selectionChange(row) {
      console.log('被选中了:', row);
    },
    async doUpdate(option, addStr = '') {
      const res = await req({
        url: this.queryOption.update.url + addStr,
        method: this.queryOption.update.method,
        ...option,
      });
      this.loadByPage();
    },
    async checkOption() {
      // 调用的重写后的方法
      this.queryOption = await this.getRequestOption();
      if (!this.queryOption)
        throw new Error(
          '混入CRUD时，必须重写getRequestOption方法，且返回值不为false等..'
        );
    },
    async doDelete(addStr) {
      console.log('.doDelete', this.queryOption);
      const res = await req({
        // url: `/loan/delete/${id}`,
        // method: 'delete',
        url: this.queryOption.delete.url + addStr,
        method: this.queryOption.delete.method,
      });
      this.loadByPage();
    },
    async loadByPage() {
      this.isLoading = true;
      //   //                        调用重写后的发放
      //   let queryOption = await this.setQueryOption();
      //   console.log('queryOption', queryOption);
      //   if (!queryOption)
      //     throw new Error(
      //       '混入CRUD时，必须重写queryOption方法，且返回值不为false等..'
      //     );
      // const res = await loanLost(pager);
      //   const res = await loanLost(this.pager);
      const res = await req({
        // url: '/loan/list',
        // method: 'get',
        url: this.queryOption.read.url,
        method: this.queryOption.read.method,
        params: this.pager,
      });
      this.isLoading = false;
      console.log('loadByPage', res?.data?.data?.list);
      // this.data = res?.data?.data?.data?.data;
      this.tableData = res?.data?.data?.list;
      //   this.configPager.total = res.data?.data.rows;
      this.setTotal(res.data?.data.rows);
      // 获取并处理total
    },
  },
};
